package com.moleq.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Base64;
import android.util.Log;

public class ImageUtil {

	// Bitmap 转化为 Bytes
	public static byte[] bitmap2Bytes(Bitmap bitmap) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bitmap.compress(CompressFormat.JPEG, 100, baos);
		byte[] bytes = baos.toByteArray();
		return bytes;
	}

	// Bitmap 转化为 String
	public static String bitmap2String(Bitmap bitmap) {
		return new String(bitmap2Bytes(bitmap));
	}

	// bitmap 转化为 Drawable
	public static Drawable bitmapToDrawable(Bitmap bitmap) {
		Drawable drawable = null;
		if (bitmap != null){
			drawable = new BitmapDrawable(bitmap);
		}
		return drawable;
	}

	// Bytes 转化为Bitmap
	public static Bitmap btyes2Bitmap(byte[] bytes) {

		ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
		Bitmap bitmap = BitmapFactory.decodeStream(bais);
		return bitmap;
	}

	// 获取图片文件的 byte 字节
	public static byte[] convertImageFile2Bytes(String path) throws IOException {

		return bitmap2Bytes(getImageFile(path));
	}

	// 将Drawable转化为Bitmap
	// public static Bitmap drawableToBitmap(Drawable drawable) {
	//
	// int width = drawable.getIntrinsicWidth();
	// int height = drawable.getIntrinsicHeight();3
	// Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
	// .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
	// : Bitmap.Config.RGB_565);// 创建一个指定高、宽的可变的Bitmap图像
	// Canvas canvas = new Canvas(bitmap);
	// drawable.setBounds(0, 0, width, height);
	// drawable.draw(canvas);
	// return bitmap;
	// }

	public static Bitmap drawableToBitmap(Drawable drawable) {

		Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
		return bitmap;
	}

	// 获取图片文件的bitmap
	public static Bitmap getImageFile(String path) throws IOException {
		FileInputStream fs = new FileInputStream(path);
		BufferedInputStream bs = new BufferedInputStream(fs);
		Bitmap btp = BitmapFactory.decodeStream(bs);

		bs.close();
		fs.close();
		return btp;
	}

	public static Bitmap loadBitmap(String url) {
		URL myFileUrl = null;
		Bitmap bitmap = null;
		try {
			myFileUrl = new URL(url);
			// HttpURLConnection conn = (HttpURLConnection)
			// myFileUrl.openConnection();
			// //conn.setDoInput(true);
			// //conn.connect();
			// InputStream is = conn.getInputStream();
			// bitmap = BitmapFactory.decodeStream(is);
			// is.close();
			// conn.disconnect();
			HttpGet httpRequest = null;

			try {
				httpRequest = new HttpGet(myFileUrl.toURI());
			} catch (URISyntaxException e) {
				e.printStackTrace();
			}

			HttpClient httpclient = new DefaultHttpClient();
			HttpResponse response = httpclient.execute(httpRequest);

			HttpEntity entity = response.getEntity();
			BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
			InputStream instream = bufHttpEntity.getContent();
			bitmap = BitmapFactory.decodeStream(instream);
		} catch (Exception e) {
			Log.e("ImageUtil", e.toString());
		}
		return bitmap;
	}

	public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2, float left, float top) {
		Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(),
				bmp1.getHeight(), bmp1.getConfig());
		Canvas canvas = new Canvas(bmOverlay);
		canvas.drawBitmap(bmp1, new Matrix(), null);
		canvas.drawBitmap(bmp2, left, top, null);
		return bmOverlay;
	}

	// 放大缩小图片文件
	// 返回byte
	public static byte[] zonmImageFile(String path, int toWidth, int toHeight)
			throws IOException {
		return bitmap2Bytes(zoomImageFile(path, toWidth, toHeight));
	}

	// 放大缩小图片
	public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
		// 获得原始图片宽高
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		Matrix matrix = new Matrix();
		// 计算缩放比（目标宽高/原始宽高）
		float scaleWidht = ((float) w / width);
		float scaleHeight = ((float) h / height);
		matrix.postScale(scaleWidht, scaleHeight);
		Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
				matrix, true);
		return newbmp;
	}

	// 放大缩小图片文件
	// 返回bitmap
	public static Bitmap zoomImageFile(String path, int toWidth, int toHeight)
			throws IOException {

		return zoomBitmap(getImageFile(path), toWidth, toHeight);
	}

	public Bitmap getPic() {
		Bitmap bitmap = BitmapFactory.decodeStream(getClass()
				.getResourceAsStream("pic.png"));
		return bitmap;
	}
	
	public static Bitmap stringtoBitmap(String string) {
		// 将字符串转换成Bitmap类型
		Bitmap bitmap = null;
		try {
			byte[] bitmapArray;
			bitmapArray = Base64.decode(string, Base64.DEFAULT);
			bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bitmap;
	}

	public static String bitmaptoString(Bitmap bitmap) {
		// 将Bitmap转换成字符串
		String string = null;
		ByteArrayOutputStream bStream = new ByteArrayOutputStream();
		bitmap.compress(CompressFormat.PNG, 100, bStream);
		byte[] bytes = bStream.toByteArray();
		string = Base64.encodeToString(bytes, Base64.DEFAULT);
		return string;
	}

}
